/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.develop.server;

import de.develop.enums.DebugTypes;
import java.util.ArrayList;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Phillip
 */
public class GameScheduler extends Thread
{

    /**
     * @todo renaming
     */
    protected ArrayList<Player> waitingClientConnections;
    protected ArrayList<Player> readyClientConnections;
    protected ArrayList<Game> playingClientConnections = new ArrayList<Game>();
    private GameServer gameServer;

    public GameScheduler( GameServer gs )
    {
        gameServer = gs;
    }

    @Override
    public void run()
    {

        while ( true )
        {
            try
            {
                Thread.sleep( 6000 );
            }
            catch ( InterruptedException ex )
            {
                Logger.getLogger( GameScheduler.class.getName() ).log( Level.SEVERE, null, ex );
            }
            processWaitingPlayers();
            processReadyPlayers();
            checkWaitingPlayers();
            checkReadyPlayers();
        }




    }

    private void checkWaitingPlayers()
    {
        debug( "checkWaitingPlayers starts..." );
        if ( waitingClientConnections.size() > 0 )
        {
            debug( "checking " + waitingClientConnections.size() + " waiting Client Connections..." );
            for ( int i = 0; i < waitingClientConnections.size(); i++ )
            {
                debug( "Index " + i + "(" + waitingClientConnections.get( i ).socket.getRemoteSocketAddress() + "): Checking" );
                if ( waitingClientConnections.get( i ).isAlive() )
                {
                    debug( "Index " + i + "(" + waitingClientConnections.get( i ).socket.getRemoteSocketAddress() + "): Alive" );
                }
                else
                {
                    debug( "Index " + i + "(" + waitingClientConnections.get( i ).socket.getRemoteSocketAddress() + "): Not Alive" );
                    debug( "Index " + i + "(" + waitingClientConnections.get( i ).socket.getRemoteSocketAddress() + ") Message: " + waitingClientConnections.get( i ).getState() );
                    waitingClientConnections.get( i ).finalize();
                    waitingClientConnections.remove( i );
                }
            }
        }
        else
        {
            debug( "no Waiting Players to check..." );
        }

    }

    private void checkReadyPlayers()
    {
        debug( "checkReadyPlayers starts..." );
        if ( readyClientConnections.size() > 0 )
        {
            debug( "checking " + readyClientConnections.size() + " ready Client Connections..." );
            for ( int i = 0; i < readyClientConnections.size(); i++ )
            {
                debug( "Index " + i + "(" + readyClientConnections.get( i ).socket.getRemoteSocketAddress() + "): Checking" );
                if ( readyClientConnections.get( i ).isAlive() )
                {
                    debug( "Index " + i + "(" + readyClientConnections.get( i ).socket.getRemoteSocketAddress() + "): Alive" );
                }
                else
                {
                    debug( "Index " + i + "(" + readyClientConnections.get( i ).socket.getRemoteSocketAddress() + "): Not Alive" );
                    debug( "Index " + i + "(" + readyClientConnections.get( i ).socket.getRemoteSocketAddress() + ") Message: " + readyClientConnections.get( i ).getState() );
                    readyClientConnections.get( i ).finalize();
                    readyClientConnections.remove( i );
                }
            }
        }
        else
        {
            debug( "no ready Players to check..." );
        }

    }

    private void processReadyPlayers()
    {
        for ( int i = 0; i < readyClientConnections.size(); i++ )
        {
            if ( i + 1 < readyClientConnections.size() )
            {
                // Was passiert wenn nur ein Spieler nicht spielt? Wird nicht beachtet
                if ( !readyClientConnections.get( i ).isIsPlaying() && !readyClientConnections.get( i + 1 ).isIsPlaying() )
                {
                    debug( "two clients are not playing...starts a new game" );
                    readyClientConnections.get( i ).setIsPlaying( true );
                    readyClientConnections.get( i + 1 ).setIsPlaying( true );

                    readyClientConnections.get(i).setOpponent(readyClientConnections.get( i + 1 ));
                    readyClientConnections.get(i + 1).setOpponent(readyClientConnections.get( i ));
                    
                    //
                    playingClientConnections.add( new Game(gameServer , readyClientConnections.get( i ), readyClientConnections.get( i + 1 ), playingClientConnections.size()-1) );
                    playingClientConnections.get( playingClientConnections.size()-1).getPlayerOne().write( "START");
                    playingClientConnections.get( playingClientConnections.size()-1).getPlayerTwo().write( "START");
                    //                                 
                }
            }

        }
    }

    private void processWaitingPlayers()
    {
        debug( "processWaitingPlayers starts..." );
        waitingClientConnections = gameServer.getWaitingClientConnections();
        readyClientConnections = gameServer.getReadyClientConnections();
        debug( "old waiting Connections: " + waitingClientConnections.size() );
        debug( "old ready Connections: " + readyClientConnections.size() );

        for ( int i = 0; i < waitingClientConnections.size(); i++ )
        {
            if ( waitingClientConnections.get( i ).isReady )
            {
                readyClientConnections.add( waitingClientConnections.get( i ) );
                if ( waitingClientConnections.remove( i ) != null )
                {
                    debug( "Client with index " + i + " successfully swaped to readList" );
                }

            }
        }
        
        
        debug( "new waiting Connections: " + waitingClientConnections.size() );
        debug( "new ready Connections: " + readyClientConnections.size() );
        debug( "processWaitingPlayers end..." );


    }

    protected void debug( String msg )
    {
        Main.debug(DebugTypes.GAME_SCHEDULER, "GameManager: ", msg );
    }
}
